/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lxj.easyadapter.sample;

import com.lxj.easyadapter.EasyAdapter;
import com.lxj.easyadapter.MultiItemTypeAdapter;
import com.lxj.easyadapter.ViewHolder;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.app.Context;
import org.jetbrains.annotations.NotNull;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class EasyAdapterOhosTest {
    private ListContainer listContainer;
    private Context mContext;

    @Before
    public void setUp() {
        mContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        listContainer = new ListContainer(mContext);
    }

    private Text createView(String text) {
        final Text textView = new Text(mContext);
        textView.setText(text);
        return textView;
    }

    private MultiItemTypeAdapter<User> setAdapterParameters() {
        List<User> userList = new ArrayList<>();
        for (int i = 0; i <= 6; ++i) {
            userList.add(new User("Position - " + i, i * 2, i));
        }
        EasyAdapter<User> adapter = new EasyAdapter<User>(userList, ResourceTable.Layout_item) {
            protected void bind(@NotNull ViewHolder holder, @NotNull User user, int position) {
                holder.setText(ResourceTable.Id_tv_name, "name: " + user.getName());
                holder.setText(ResourceTable.Id_tv_age, "age: " + user.getAge());
            }
        };
        adapter.addHeaderView(createView("Header view 1"));
        adapter.addHeaderView(createView("Header view 2"));
        adapter.addFootView(createView("Footer view 1"));
        adapter.addFootView(createView("Footer view 2"));
        adapter.addFootView(createView("Footer view 3"));

        listContainer.setItemProvider(adapter);
        return adapter;
    }

    @Test
    public void testBundleName() {
        final String actualBundleName = AbilityDelegatorRegistry.getArguments().getTestBundleName();
        assertEquals("com.lxj.easyadapter.sample", actualBundleName);
    }

    @Test
    public void testMultiItemHeadersCount() {
        MultiItemTypeAdapter<User> multiItemTypeAdapter = setAdapterParameters();
        assertEquals(2, multiItemTypeAdapter.getHeadersCount());
    }

    @Test
    public void testMultiItemFootersCount() {
        MultiItemTypeAdapter<User> multiItemTypeAdapter = setAdapterParameters();
        assertEquals(3, multiItemTypeAdapter.getFootersCount());
    }

    @Test
    public void testMultiItemTotalCount() {
        MultiItemTypeAdapter<User> multiItemTypeAdapter = setAdapterParameters();
        assertEquals(12, multiItemTypeAdapter.getCount());
    }

    @Test
    public void testMultiItemHeaderText() {
        MultiItemTypeAdapter<User> multiItemTypeAdapter = setAdapterParameters();
        assertEquals("Header view 1", ((Text) multiItemTypeAdapter.getHeaderView(0)).getText());
        assertEquals("Header view 2", ((Text) multiItemTypeAdapter.getHeaderView(1)).getText());
    }

    @Test
    public void testMultiItemFooterText() {
        MultiItemTypeAdapter<User> multiItemTypeAdapter = setAdapterParameters();
        assertEquals("Footer view 1", ((Text) multiItemTypeAdapter.getFooterView(0)).getText());
        assertEquals("Footer view 2", ((Text) multiItemTypeAdapter.getFooterView(1)).getText());
        assertEquals("Footer view 3", ((Text) multiItemTypeAdapter.getFooterView(2)).getText());
    }

    @Test
    public void testMultiItemUserText() {
        MultiItemTypeAdapter<User> multiItemTypeAdapter = setAdapterParameters();
        assertEquals("Position - 0", ((User) multiItemTypeAdapter.getItem(0)).getName());
    }
}